﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Web_search : System.Web.UI.Page
{
    protected List<Train> ts;
    protected string content = string.Empty;
    protected int pages = 1;
    protected int all_page = 0;
    private int size = 16;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        
        CmsDataContext db = new CmsDataContext();
        ///
        // 初始化
        ///
        var train = (from aT in db.Train where (aT.Started - DateTime.Now).Days >= 0 orderby aT.Registed descending select aT).Take(6);

        ts = train.ToList<Train>();
        var px2 = (from aA in db.Article where aA.Verified != null orderby aA.Verified descending select aA).Take(6);
        Repeater3.DataSource = px2;
        Repeater3.DataBind();


        content = Session["content"].ToString();
        //Response.Write("传过啦的字："+word);

        var a = (from aA in db.Article where aA.Body.Contains(content)||aA.Title.Contains(content)&&aA.Verified!=null select aA).Distinct<Article>();
        //List<Article> art = new List<Article>();
        //art = wS.ToList<Article>();
        //Repeater1.DataSource = a;
        //Repeater1.DataBind();
        if (a.Count() % size == 0)
        {
            all_page = a.Count() / size;
        }
        else
        {
            all_page = a.Count() / size + 1;
        }
        Paging(content, 1);
        
    }
    protected string GetTopicID(string tName)
    {
        CmsDataContext db = new CmsDataContext();
        var t = from aT in db.Topic where aT.Name.Equals(tName) select aT;
        if (t.Count() != 0)
        {
            return t.First().ID;
        }
        else
        {
            return "";
        }
    }
    protected void FirstButton_Click(object sender, ImageClickEventArgs e)
    {
        pages = 1;
        string content = Session["content"].ToString();
        Paging(content, pages);
    }
    protected void PreviousButton_Click(object sender, ImageClickEventArgs e)
    {

        if (pages > 1)
        {
            pages--;
        }
        else
        {
            pages = 1;
        }
        string content = Session["content"].ToString();
        Paging(content, pages);
    }
    protected void NextButton_Click(object sender, ImageClickEventArgs e)
    {
        if (pages < all_page)
        {
            pages++;
        }
        else
        {
            pages = all_page;
        }
        string content = Session["content"].ToString();
        Paging(content, pages);
    }
    protected void LastButton_Click(object sender, ImageClickEventArgs e)
    {
        pages = all_page;
        string content = Session["content"].ToString();
        Paging(content, pages);
    }
    protected void Paging(string topic_id, int page)
    {
        if (page != 0)
        {
            CmsDataContext db = new CmsDataContext();
            var a = (from aA in db.Article where aA.Body.Contains(content) || aA.Title.Contains(content) && aA.Verified != null orderby aA.Published descending select aA).Distinct<Article>().Skip((page - 1) * size).Take(size);
            Repeater1.DataSource = a;
            Repeater1.DataBind();
        }
        else
        {
            pages = 1;
            all_page = 0;
        }
    }
}